package com.example.keli.ui.my.commission;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.keli.R;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.BaseActivity;
import com.example.keli.base.app.C;
import com.example.keli.base.utils.DialogUtil;
import com.example.keli.base.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;

/**
 * description 提成
 * created by jerry on 2019/3/18.
 */
public class DishActivity extends BaseActivity {

    public static final int REQUEST_EDIT_COUNT = 1;
    @BindView(R.id.rootView)
    LinearLayout rootView;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.tv_edit)
    TextView tvEdit;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_money)
    TextView tvMoney;

    private String mCount;
    private String mName;
    private String mDishNum;

    @Override
    protected int layoutId() {
        return R.layout.activity_dish;
    }

    @Override
    protected void bindData() {
//        setTitle("提现");
        abTitle.setText("提现");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mDishNum = getIntent().getStringExtra("dish_num");
        mCount = getIntent().getStringExtra("alipay_count");
        mName = getIntent().getStringExtra("alipay_name");
        tvCount.setText(StringUtils.dealPhoneNumber(mCount));
        tvName.setText(StringUtils.dealName(mName));
        tvMoney.setText("￥"+mDishNum);
    }

    @OnClick({R.id.tv_edit, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_edit:
                startActivityForResult(new Intent(this, AlipayEditActivity.class), 1);
                break;
            case R.id.tv_submit:
                showTipDialog();
            break;
        }
    }

    private void showTipDialog(){
        View view = getLayoutInflater().inflate(R.layout.dish_tip_dialog,null);
        TextView tv_dish_total = view.findViewById(R.id.tv_dish_total);
        TextView tv_dish_available = view.findViewById(R.id.tv_dish_available);
        TextView tv_dish_fee = view.findViewById(R.id.tv_dish_fee);
        TextView tv_dish = view.findViewById(R.id.tv_dish);
        tv_dish_total.setText(mDishNum+"元");
        double dish = Double.valueOf(mDishNum);
        tv_dish_available.setText(String.format("%.2f", dish*0.8)+"元");
        tv_dish_fee.setText(String.format("%.2f", dish*0.2)+"元");
        tv_dish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindAli();
            }
        });
        DialogUtil.getMenuDialog(DishActivity.this,view).show();
    }

    private void bindAli(){
        Map<String, String> map = new HashMap<>();
        map.put("user_name", mName);
        map.put("ali_address", mCount);
        RetrofitManager.ApiFunc(RetrofitManager.mApiFunc.setUser(map), new SubscriberListener<BaseResponse<Object>>() {
            @Override
            public void onSuccess(BaseResponse<Object> objectBaseResponse) {
                dish();
            }

            @Override
            public void onFailer(String msg) {
                toast(msg);
            }

            @Override
            public void onTokenError() {

            }
        });
    }

    private void dish(){
        startProgressDialog("正在申请提现");
        RetrofitManager.ApiFunc(RetrofitManager.mApiService.dish(), new SubscriberListener<BaseResponse<Object>>() {
            @Override
            public void onSuccess(BaseResponse<Object> baseResponse) {
                stopProgressDialog();
                toast(baseResponse.getMsg());
                EventBus.getDefault().post(C.EVENT.DISH_FINISH);
                finish();
            }

            @Override
            public void onFailer(String msg) {
                stopProgressDialog();
                toast(msg);
            }

            @Override
            public void onTokenError() {
                stopProgressDialog();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null && data.getExtras() != null){
            mCount = data.getExtras().getString("alipay_count");
            mName = data.getExtras().getString("alipay_name");
            tvCount.setText(StringUtils.dealPhoneNumber(mCount));
            tvName.setText(StringUtils.dealName(mName));
        }
    }
}
